package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcCsunCameraWebRtc;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcWebRtcClient;
import com.csun.cmny.provider.model.dto.webrtc.WebRtcClientQueryDto;
import com.csun.cmny.provider.model.vo.webrtc.WebRtcClientVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunCameraWebRtcService;
import com.csun.cmny.provider.service.DmcWebRtcClientService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020/7/21 17:19
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/webRtcClient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcWebRtcClientController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcWebRtcClientService dmcWebRtcClientService;
    @Resource
    private DmcCsunCameraWebRtcService dmcCsunCameraWebRtcService;

    /**
     * 查询设备编号
     *
     * @param webRtcClientQueryDto
     *
     * @return
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody WebRtcClientQueryDto webRtcClientQueryDto) {

        List<WebRtcClientVo> webRtcClientVoList = dmcWebRtcClientService.queryListWithPage(webRtcClientQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(webRtcClientVoList));
    }

    /**
     * 保存
     *
     * @param dmcWebRtcClient
     *
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@RequestBody DmcWebRtcClient dmcWebRtcClient) {

        Integer result = dmcWebRtcClientService.save(dmcWebRtcClient, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 根据Id删除
     *
     * @param id
     *
     * @return
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcWebRtcClientService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 绑定
     *
     * @param dmcCsunCameraWebRtc
     *
     * @return
     */
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Wrapper bind(@RequestBody DmcCsunCameraWebRtc dmcCsunCameraWebRtc) {

        Integer result = dmcWebRtcClientService.bind(dmcCsunCameraWebRtc);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 解绑
     *
     * @param dmcCsunCameraWebRtc
     *
     * @return
     */
    @RequestMapping(value = "/untied", method = RequestMethod.POST)
    public Wrapper untied(@RequestBody DmcCsunCameraWebRtc dmcCsunCameraWebRtc) {

        Integer result = dmcWebRtcClientService.untied(dmcCsunCameraWebRtc);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    @RequestMapping(value = "/queryNotBindCameraList", method = RequestMethod.POST)
    public Wrapper queryNotBindCameraList() {

        List<DmcDevice> dmcDeviceList = dmcCsunCameraWebRtcService.queryNotBindCameraList();

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), dmcDeviceList);
    }

}